﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SyncLib.Interface;
using Microsoft.Synchronization;
using System.Windows.Forms;
using System.Diagnostics;

namespace SyncLib.Client
{
    public class ClientSyncContext
    {
        public static IUrlProvider UrlProvider { get; set; }
        public static SyncOrchestrator CurrentAgent { get; set; }
        private static object LockObject;
        public static bool IsRunning { get; set; }
        public static string ScopeName { get; set; }
        public static ClientSyncDS SyncDS;// { get; set; }
        public static ClientSyncDS.TaskSyncListRow CurrentTask ;//{ get; set; }
        private static int taskIndex;
        public static int TaskIndex { get{
            return taskIndex;
        }
            set
            {
                Debug.WriteLine("Set Task Index :" + value);
                taskIndex = value;
            }
        }
        public static Dictionary<string, Image> TaskImage { get; set; }
        private static Dictionary<string, SyncDirectionOrder> ForeScopeDirection;
        public static volatile bool UserCancel = false;
        public static bool CreateProxyPerTask = false;
        public static void AddScopeToTaskList(string scopeName)
        {
            ClientSyncContext.SyncDS.TaskSyncList.AddTaskSyncListRow(ClientSyncContext.SyncDS.TaskSyncList.Count
                , scopeName, ClientConst.DesTaskPending, "", TaskImage["Pending"], "", ClientConst.TaskPending, null,Guid.Empty,"");

        }
        public static void AddScopeToTaskList(string scopeName, object tag)
        {
            ClientSyncContext.SyncDS.TaskSyncList.AddTaskSyncListRow(ClientSyncContext.SyncDS.TaskSyncList.Count
                , scopeName, ClientConst.DesTaskPending, "", TaskImage["Pending"], "", ClientConst.TaskPending, tag,Guid.Empty,"");

        }
        public static void CancelRemainingSyncTask()
        {
            lock (LockObject)
            {

                foreach (ClientSyncDS.TaskSyncListRow task in SyncDS.TaskSyncList)
                {
                    if (task.Status == ClientConst.TaskPending)
                    {
                        task.Status = ClientConst.DesTaskCancel;
                        task.Image = TaskImage[ClientConst.TaskCancel];
                        task.Message = "Người dùng hủy thao tác";
                        CurrentTask.StatusCode = ClientConst.TaskCancel;
                    }
                }
                UserCancel = true;
            }
        }
        public static void AppIssue(string type, string message, string detail)
        {
            SyncDS.AppIssue.AddAppIssueRow(type, message, detail, DateTime.Now, null);
            try
            {
                SyncDS.AppIssue.WriteXml(Application.StartupPath + "\\AppIssue.xml");
            }
            finally
            {
            }
        }
        public static void AddForeDirection(SyncDirectionOrder direct, string scopeConfig)
        {
            List<string> lst = new List<string>(scopeConfig.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries));
            if (lst.Contains("*"))
            {
                if (ForeScopeDirection.ContainsKey("*"))
                    ForeScopeDirection["*"] = direct;
                else
                {
                    ForeScopeDirection.Clear();
                    ForeScopeDirection.Add("*", direct);
                }
            }
            else
                foreach (string node in lst)
                {
                    if (ForeScopeDirection.ContainsKey(node))
                        ForeScopeDirection[node] = direct;
                    else
                    {
                        // ForeScopeDirection.Clear();
                        ForeScopeDirection.Add(node, direct);
                    }
                }
        }
        public static int GetForeDirection(string scopeName)
        {
            if (ForeScopeDirection.ContainsKey("*"))
                return (int)ForeScopeDirection["*"];

            if (ForeScopeDirection.ContainsKey(scopeName))
                return (int)ForeScopeDirection[scopeName];
            else
                return -1;
        }
        public static event EventHandler<PerformSyncArg> SyncProcess;

        public static void OnSyncProcess(EventType type)
        {
            if (SyncProcess != null)
            {
                PerformSyncArg e = new PerformSyncArg();
                e.Type = type;
                SyncProcess(null, e);

            }
        }
        public void OnStartPerformSynchronize(object sender, EventArgs e)
        {
        }
        static ClientSyncContext()
        {
            LockObject = new object();
            TaskImage = new Dictionary<string, Image>();
            TaskImage.Add("Pending", Image.FromFile(Application.StartupPath + "\\Animation\\Pending.png"));
            TaskImage.Add("Error", Image.FromFile(Application.StartupPath + "\\Animation\\Error.png"));
            TaskImage.Add("Success", Image.FromFile(Application.StartupPath + "\\Animation\\Success.png"));
            TaskImage.Add("Warning", Image.FromFile(Application.StartupPath + "\\Animation\\Warning.png"));
        }

    }

    public class PerformSyncArg : EventArgs
    {
        public EventType Type { get; set; }
    }
    public enum EventType
    {
        BeginSync,
        BeginTask,
        FinishSync,
        FinishTask

    }

}
